package com.eyas.business.controllor.wechat;

import com.eyas.business.model.jdbcEntity.table.ChildrenInfo;
import com.eyas.business.model.jdbcEntity.table.ClassInfo;
import com.eyas.business.model.jdbcEntity.table.UserInfo;
import com.eyas.business.model.pojo.appointmentinfo.QueryResultDTO;
import com.eyas.business.model.pojo.recordinfo.PageQueryResult;
import com.eyas.business.model.pojo.userinfo.QueryPageParmDTO;
import com.eyas.business.model.pojo.userinfo.QueryPageResultDTO;
import com.eyas.business.model.pojo.webchat.AppointMentInfoDTO;
import com.eyas.business.model.pojo.webchat.UserRegisterDTO;
import com.eyas.business.model.pojo.webchat.YKParamDTO;
import com.eyas.business.service.RecordInfo.RecordInfoService;
import com.eyas.business.service.appointmentinfo.AppointmentInfoService;
import com.eyas.business.service.classinfo.ClassInfoService;
import com.eyas.business.service.userinfo.UserInfoService;
import com.eyas.core.json.AjaxJson;
import com.eyas.core.model.page.PageResult;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

/**小程序对接接口
 * @Auther: 王龙龙
 * @Date: 2019/3/18 15:30
 * @Description:
 */
@Controller
@RequestMapping("/eyas/wechat/")
public class WechatAction {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private ClassInfoService classInfoService;
    @Autowired
    private RecordInfoService recordInfoService;
    @Autowired
    private AppointmentInfoService appointmentInfoService;

	/**
	 * 查询所有会员
	 * @param dto
	 * @param page
	 * @param limit
	 * @return
	 */
    @GetMapping("/queryUserInfo")
    @ResponseBody
    public AjaxJson queryUserInfo(QueryPageParmDTO dto,
                                                            @RequestParam(defaultValue = "1") int page,
                                                            @RequestParam(defaultValue = "10") int limit) {
        AjaxJson json=new AjaxJson();
    	PageResult<QueryPageResultDTO> pageResult = userInfoService.queryUserInfo(dto,page-1,limit);
        json.put("list", pageResult.getData());
        return json;
    }

    @PostMapping("/changeValidflag")
    @ResponseBody
    public String changeValidflag(int userid) {
        userInfoService.changeValidflag(userid);
        return "{\"success\":true}";
    }
    /**
     * 查询会员个人信息
     * @return
     */
	@GetMapping("/getUserInfoByOpenid")
	@ResponseBody
	public AjaxJson getUserInfoByOpenid(String openid ) {
		AjaxJson json = new AjaxJson();
		if (openid == null || openid.equals("")) {
			json.setSuccess(false);
			json.setMsg("openid不能为空");
			return json;
		}
		UserInfo userInfo = this.userInfoService.getUserInfoByOpenid(openid);
		if (userInfo != null) {
            QueryPageResultDTO queryPageResultDTO = new QueryPageResultDTO();
            BeanUtils.copyProperties(userInfo,queryPageResultDTO);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            if(queryPageResultDTO.getDeadline()!=null) {
                if(simpleDateFormat.format(queryPageResultDTO.getDeadline())
                        .compareTo(simpleDateFormat.format(new Date()))<0) {
                    queryPageResultDTO.setDlvalidflag("0"); //有效截止期在系统日期之前，会员已经过期
                }else {
                    queryPageResultDTO.setDlvalidflag("1"); //会员未过期
                }
            }else {
                queryPageResultDTO.setDlvalidflag("1");//有效截止期为空，就认为是长期有效会员，不过期
            }
			json.put("userInfo", queryPageResultDTO);
		} else {
			json.setSuccess(false);
			json.setMsg("未注册，请先注册账号！");
		}
		return json;
	}
    /**
     * 会员注册
     * @return
     */
    /*@GetMapping("/register")
    @ResponseBody
    public AjaxJson register(QueryPageParmDTO dto,HttpSession session) {
    	AjaxJson json=new AjaxJson();
    	userInfoService.saveUserInfo(dto,session);
        return json;
    }*/
    @PostMapping("/register")
    @ResponseBody
    public AjaxJson register(@RequestBody  UserRegisterDTO dto, HttpSession session) {
        AjaxJson json=new AjaxJson();
        userInfoService.saveUserInfoPublic(dto,session);
        return json;
    }

    @PostMapping("/saveUserInfo")
    @ResponseBody
    public String saveUserInfo(QueryPageParmDTO dto, HttpSession session) {
        userInfoService.saveUserInfo(dto,session);
        return "{\"success\":true}";
    }

    @PostMapping("/saveChildrenInfo")
    @ResponseBody
    public String saveUserInfo(@RequestBody ChildrenInfo childrenInfo) {
        userInfoService.saveChildrenInfo(childrenInfo);
        return "{\"success\":true}";
    }

    @GetMapping("/queryChildrenInfoByUserId")
    @ResponseBody
    public AjaxJson queryChildrenInfoByUserId(Integer userid) {
        List<ChildrenInfo> list = userInfoService.queryChildrenInfoByUser(userid);
        AjaxJson json=new AjaxJson();
        json.put("list", list);
        return json;
    }
    
    /**
     * 查询所有课程
     * @return
     */
    @GetMapping("/queryClassInfoList")
    @ResponseBody
    public AjaxJson queryClassInfoList(Integer userid) {
    	AjaxJson json=new AjaxJson();
    	json.put("list", classInfoService.queryClassInfoList(userid));
        return json;
    }

    /**
     * 获取课程详情
     * @param classid
     * @return
     */
    @GetMapping("/getClassInfoByClassID")
    @ResponseBody
    public AjaxJson edit(int classid) {
    	AjaxJson json=new AjaxJson();
        ClassInfo classInfo = this.classInfoService.getClassInfoByClassID(classid);
        json.put("classinfo",classInfo);
        return json;
    }

    /**
     * 开课列表
     * @param selectDate
     * @return
     */
    @GetMapping("/queryRecordInfoForPage")
    @ResponseBody
    public AjaxJson queryRecordInfoForPage(Integer classid,@DateTimeFormat(pattern = "yyyy-MM-dd") Date selectDate) {
        List<PageQueryResult> pageQueryResultPageResult = this.recordInfoService.queryRecordInfoForList(classid,selectDate);
        AjaxJson json=new AjaxJson();
        json.put("list", pageQueryResultPageResult);
        return json;
    }


    /**
     * 会员约课
     * @return
     */
    @PostMapping("/saveAppointmentInfo")
    @ResponseBody
    public String saveAppointmentInfo(@RequestBody YKParamDTO ykParamDTO) {
        com.eyas.business.model.pojo.appointmentinfo.YKParamDTO ykParamDTO1 = new com.eyas.business.model.pojo.appointmentinfo.YKParamDTO();
        BeanUtils.copyProperties(ykParamDTO,ykParamDTO1,"childrens");
        ykParamDTO1.setChildrens(new Integer[1]);
        ykParamDTO1.getChildrens()[0] = ykParamDTO.getChildrens();
        this.appointmentInfoService.saveAppointmentInfoPublic(ykParamDTO1);
        return "{\"success\":true}";
    }

    /**
     * 会员约课历史记录
     * @param userid  会员ID
     * @return
     */
    @GetMapping("/queryAppointmentInfo")
    @ResponseBody
    public AjaxJson queryAppointmentInfo(Integer userid) {
        List<QueryResultDTO> result = this.appointmentInfoService.queryAppointmentInfoPublic(userid);
        AjaxJson json=new AjaxJson();
        json.put("list", result);
        return json;
    }

    /**
     * 会员取消约课
     * @param paramdto
     * @return
     */
    @PostMapping("/updateAppointmentInfo")
    @ResponseBody
    public String updateAppointmentInfo(@RequestBody AppointMentInfoDTO paramdto) {
        this.appointmentInfoService.updateAppointmentInfoPublic(paramdto);
        return "{\"success\":true}";
    }
}
